/*
 * Copyright (c) 2023 - present Dromara, All rights reserved.
 *
 *   https://visor.dromara.org
 *   https://visor.dromara.org.cn
 *   https://visor.orionsec.cn
 *
 * Members:
 *   Jiahang Li - ljh1553488six@139.com - author
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ruoyi.visor.module.asset.service.impl;

import lombok.extern.slf4j.Slf4j;
import com.ruoyi.visor.common.constant.Const;
import com.ruoyi.visor.common.constant.ErrorMessage;
import com.ruoyi.visor.common.utils.Valid;
import com.ruoyi.visor.framework.biz.operator.log.core.utils.OperatorLogs;
import com.ruoyi.visor.framework.redis.core.utils.RedisMaps;
import com.ruoyi.visor.framework.redis.core.utils.RedisUtils;
import com.ruoyi.visor.framework.redis.core.utils.barrier.CacheBarriers;
import com.ruoyi.visor.module.asset.convert.HostKeyConvert;
import com.ruoyi.visor.module.asset.dao.HostConfigDAO;
import com.ruoyi.visor.module.asset.dao.HostIdentityDAO;
import com.ruoyi.visor.module.asset.dao.HostKeyDAO;
import com.ruoyi.visor.module.asset.define.cache.HostCacheKeyDefine;
import com.ruoyi.visor.module.asset.entity.domain.HostKeyDO;
import com.ruoyi.visor.module.asset.entity.dto.HostKeyCacheDTO;
import com.ruoyi.visor.module.asset.entity.vo.HostKeyVO;
import com.ruoyi.visor.module.asset.service.HostKeyService;
import com.ruoyi.visor.module.infra.api.DataExtraApi;
import com.ruoyi.visor.module.infra.api.DataPermissionApi;
import com.ruoyi.visor.module.infra.enums.DataPermissionTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 主机密钥 服务实现类
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2023-9-20 11:55
 */
@Slf4j
@Service
public class HostKeyServiceImpl implements HostKeyService {

    @Resource
    private HostKeyDAO hostKeyDAO;

    @Resource
    private HostIdentityDAO hostIdentityDAO;

    @Resource
    private HostConfigDAO hostConfigDAO;

    @Resource
    private DataExtraApi dataExtraApi;

    @Resource
    private DataPermissionApi dataPermissionApi;

    @Override
    public List<HostKeyVO> getHostKeyList() {
        // 查询缓存
        List<HostKeyCacheDTO> list = RedisMaps.valuesJson(HostCacheKeyDefine.HOST_KEY);
        if (list.isEmpty()) {
            // 查询数据库
            list = hostKeyDAO.of().list(HostKeyConvert.MAPPER::toCache);
            // 设置屏障 防止穿透
            CacheBarriers.checkBarrier(list, HostKeyCacheDTO::new);
            // 设置缓存
            RedisMaps.putAllJson(HostCacheKeyDefine.HOST_KEY, s -> s.getId().toString(), list);
        }
        // 删除屏障
        CacheBarriers.removeBarrier(list);
        // 转换
        return list.stream()
                .map(HostKeyConvert.MAPPER::to)
                .sorted(Comparator.comparing(HostKeyVO::getId))
                .collect(Collectors.toList());
    }


}
